package com.personnel.service.impl;

import com.personnel.mail.MyJavaMail;
import com.personnel.mapper.StaffMapper;
import com.personnel.pojo.Staff;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YuePeng
 * @Date: 2022/3/24 15:22
 * @Version: 1.0
 */
@Service
public class MailCheckImpl {

    @Resource
    private StaffMapper staffMapper;

    @Transactional
    public Map<String, Object> sendMailCode(String email) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "发送失败！");
        try {
            MyJavaMail myJavaMail = new MyJavaMail("EmailRobot", email);
            new Thread(myJavaMail).start();   // 发送邮件
            String uuid = myJavaMail.getUuid();
            Staff staff = new Staff();
            staff.setUuid(uuid);
            staff.setEmail(email);
            this.staffMapper.updateByEmail(staff);
            map.put("code", 200);
            map.put("msg", "发送成功,请查看你的邮箱！");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public Map<String, Object> checkCode(String code) {
        Staff staff = this.staffMapper.selectByUuid(code);
        Map<String, Object> map = new HashMap<>();
        if (staff != null) {
            map.put("code", 200);
            map.put("msg", "验证成功,请查看你的邮箱！");
            map.put("data",staff);
        }else {
            map.put("code", 500);
            map.put("msg", "验证失败！");
        }
        return map;
    }
}
